<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="上级用户名" prop="userName">
        <el-input v-model="queryParams.superiorUserName" placeholder="请输入上级用户名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户手机号" prop="userPhone">
        <el-input v-model="queryParams.userPhone" placeholder="请输入用户手机号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="会员等级" prop="gradeCode">
        <el-input v-model="queryParams.gradeCode" placeholder="请输入会员等级" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--<el-form-item label="用户状态" prop="userStatus">
        <el-select v-model="queryParams.userStatus" placeholder="请选择用户状态" clearable size="small">
          <el-option label="&#45;&#45;请选择&#45;&#45;" value="" />
          <el-option v-for="item in userStatusList" :key="item.code" :value="item.code" :label="item.userStatus" />
        </el-select>
      </el-form-item>
      <el-form-item label="实名认证状态" prop="autonymType">
        <el-select v-model="queryParams.autonymType" placeholder="请选择实名认证状态" clearable size="small">
          <el-option label="&#45;&#45;请选择&#45;&#45;" value="" />
          <el-option v-for="item in userStatusList" :key="item.code" :value="item.code" :label="item.value" />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['pos:TUserInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="TUserInfoList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="用户id" align="center" prop="userId" v-if="false" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="用户头像" align="center" prop="userImg">
        <template slot-scope="scope">
          <el-popover placement="left" trigger="click" width="400">
            <img :src="scope.row.userImg" width="100%" />
            <img slot="reference" :src="scope.row.userImg" :alt="scope.row.userImg"
              style="max-height: 50px;max-width: 50px; padding: 5px" />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="用户手机号" align="center" prop="userPhone" />
      <el-table-column label="用户状态" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.userStatus" active-value="0" inactive-value="1"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="总余额" align="center" prop="balanceSum" />
      <el-table-column label="已提现余额" align="center" prop="balanceWithdraw" />
      <el-table-column label="已使用余额" align="center" prop="balanceUse" />
      <el-table-column label="上级用户名称" align="center" prop="superiorUserName" />
      <el-table-column label="用户等级" align="center" prop="gradeCode" />
      <el-table-column label="是否实名认证" align="center" prop="autonymType" />
      <el-table-column label="趣工宝会员编号" align="center" prop="memberNo" />
      <el-table-column label="注册时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px;">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="changeGrade(scope.row)" v-if="scope.row.gradeCode == '核心V9'? false:true"
            v-hasPermi="['pos:TUserInfo:edit']">变更等级</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['pos:TUserInfo:edit']">修改用户信息</el-button>-->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="changeTeam(scope.row)"
            v-hasPermi="['pos:TUserInfo:edit']">变更团队</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)"
            v-hasPermi="['pos:TUserInfo:query']">修改用户信息</el-button>
          <!--<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateBankCard(scope.row)" v-if="scope.row.memberNo == null? false:true"
            v-hasPermi="['pos:TUserInfo:query']">同步趣工宝</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 变更会员等级基础数据对话框 -->
    <el-dialog :title="title" :visible.sync="gradeDiaLog" width="50%;" append-to-body>
      <el-form ref="gradeForm" :model="gradeForm" label-width="80px">
        <el-input v-model="gradeForm.userId" placeholder="请输入用户名" v-if="false" />
        <el-input v-model="gradeForm.gradeId" placeholder="请输入用户名" v-if="false" />
      </el-form>
      <el-table v-loading="loading" :data="gradeList">
        <el-table-column type="radio" width="55" align="center" />
        <el-table-column label="会员id" align="center" prop="id" v-if="false" />
        <el-table-column label="会员等级" align="center" prop="gradeCode" />
        <el-table-column label="会员中文" align="center" prop="gradeName" />
        <el-table-column label="操作" align="center" prop="gradeName">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-detail" @click="submitGradeForm(scope.row)"
              v-hasPermi="['pos:TUserInfo:edit']">确定</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--<div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitGradeForm()">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>-->
    </el-dialog>

    <!-- 变更团队基础数据对话框 -->
    <el-dialog :title="title" :visible.sync="teamDiaLog" width="80%;" append-to-body>
      <el-form :model="queryTeamParams" ref="queryTeamParams" :inline="true" v-show="showSearch">
        <el-input v-model="queryTeamParams.userId" placeholder="请输入用户名" v-if="false" />
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="queryTeamParams.userName" placeholder="请输入用户名" clearable size="small"
            @keyup.enter.native="handleTeamQuery" />
        </el-form-item>
        <el-form-item label="用户手机号" prop="userPhone">
          <el-input v-model="queryTeamParams.userPhone" placeholder="请输入用户手机号" clearable size="small"
            @keyup.enter.native="handleTeamQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleTeamQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQueryTeam">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="teamList">
        <el-table-column label="用户id" align="center" prop="userId" v-if="false" />
        <el-table-column label="用户名" align="center" prop="userName" />
        <el-table-column label="用户手机号" align="center" prop="userPhone" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-detail" @click="submitTeamForm(scope.row)"
              v-hasPermi="['pos:TUserInfo:edit']">确定</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryTeamParams.pageNum" :limit.sync="queryTeamParams.pageSize"
                  @pagination="getTeamList" />
    </el-dialog>

<!--    <el-dialog :title="title" :visible.sync="open" width="50%;" append-to-body>
      <el-form ref="form" :model="form"  label-width="80px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="用户名" disabled/>
        </el-form-item>
        <el-form-item label="用户头像" prop="userImg">
          <el-input v-model="form.userImg" placeholder="用户头像" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitGradeForm()">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>-->

  </div>
</template>

<script>
  import {
    queryUserStatus,
    listTUserInfo,
    addTUserInfo,
    updateTUserInfo,
    exportTUserInfo,
    handleStatusChange,
    getGradeInfoList,
    changeUserTeam,
    updateBankCard
  } from "@/api/pos/TUserInfo";

  export default {
    name: "TUserInfo",
    data() {
      return {
        userId : null,
        userStatusList: [],
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户基础数据表格数据
        TUserInfoList: [],
        gradeList: [],
        teamList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        gradeDiaLog: false,
        teamDiaLog: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: null,
          userPhone: null,
          gradeCode: null
        },
        // 表单参数
        form: {},
        gradeForm: {
          userId: null,
          gradeId: null,
          gradeCode: null
        },
        queryTeamParams: {
          pageNum: 1,
          pageSize: 10,
          userName: null,
          userPhone: null,
          userId: null,
          superiorUserId: null
        },
      };
    },
    created() {
      this.getList();
      this.getUserStatus();
    },
    methods: {
      /** 查询用户基础数据列表 */
      getList() {
        this.loading = true;
        listTUserInfo(this.queryParams).then(response => {
          this.TUserInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      getTeamList() {
        listTUserInfo(this.queryTeamParams).then(response => {
          this.teamList = response.rows;
          this.total = response.total;
          this.teamDiaLog = true;
          this.loading = false;
          // this.queryTeamParams.userId = row.userId;

          this.title = "变更团队列表";
        });
      },

      getUserStatus(){
        queryUserStatus().then(response => {
          this.userStatusList = response.data;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          userName: null,
          userPhone: null,
          gradeCode: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      handleTeamQuery() {
        this.queryTeamParams.pageNum = 1;
        this.getTeamList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 重置按钮操作 */
      resetQueryTeam() {
        this.resetForm("queryTeamParams");
        this.queryTeamParams.userId = null;
        this.handleTeamQuery();
      },
      /** 禁用/启用用户 */
      handleStatusChange(row) {
        let text = row.userStatus === "0" ? "启用" : "禁用";
        this.$modal.confirm('确认要"' + text + '""' + row.userName + '"吗？').then(function() {
          return handleStatusChange(row.userId, row.userStatus);
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function() {
          row.userStatus = row.userStatus === "0" ? "1" : "0";
        });
      },
      /** 查询详情按钮操作 */
      handleDetail(row) {
        const userId = row.userId || this.ids[0];
        this.$router.push({
          path: "/TUserInfo/userManage/userDetail",
          query: {
            userId: userId
          }
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.userId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 变更会员等级按钮操作 */
      changeGrade(row) {
        const userId = row.userId || this.ids
        getGradeInfoList(userId).then(response => {
          //console.log(response.data);
          this.gradeList = response.data;
          this.gradeDiaLog = true;
          this.gradeForm.userId = userId;
          this.title = "查询会员等级列表";
        });
      },
      /** 变更会员等级提交按钮 */
      submitGradeForm(row) {
        this.gradeForm.gradeId = row.id;
        //const userId = this.gradeForm.userId;
        //this.$message.info(userId + "-----------" + row.id);
        this.$refs["gradeForm"].validate(valid => {
          if (valid) {
            updateTUserInfo(this.gradeForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.gradeDiaLog = false;
              this.getList();
            });
          }
        });
      },
      /** 变更团队按钮操作 */
      changeTeam(row) {
        this.userId = row.userId;
        this.getTeamList();
      },
      /** 变更团队关系提交按钮 */
      submitTeamForm(row) {
        this.queryTeamParams.userName = null;
        this.queryTeamParams.userPhone = null;
        this.queryTeamParams.superiorUserId = row.userId;
        this.queryTeamParams.userId = this.userId;
        //this.$message.info(this.queryTeamParams.superiorUserId + "-----------" + row.userId);
        changeUserTeam(this.queryTeamParams).then(response => {
          this.queryTeamParams.userId = null;
          this.$modal.msgSuccess("变更成功");
          this.teamDiaLog = false;
          this.getList();
        });
      },
      handleUpdateBankCard(row) {
        this.$confirm('确定同步将银行卡信息同步到趣工宝信息吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.form.userId = row.userId || this.ids;
          updateBankCard(this.form).then(response => {
            this.$modal.msgSuccess("同步成功");
            this.getList();
          });
        }).catch(() => {});
      },
      /** 修改按钮操作 */
/*      handleUpdate(row) {
        this.reset();
        const userId = row.userId || this.ids
        getTUserInfo(userId).then(response => {
          console.log(response)
          this.form = response.data;
          this.open = true;
          this.title = "修改用户信息";
        });
      },*/
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.userId != null) {
              updateTUserInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addTUserInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$modal.confirm('是否确认导出所有用户基础数据数据项？').then(() => {
          this.exportLoading = true;
          return exportTUserInfo(queryParams);
        }).then(response => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
